#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10, M = 2 * N;
int res, cnt;
int n, m;
int f[N];
int find(int a)
{
	if (a != f[a]) {
		f[a] = find(f[a]);
	}
	return f[a];
}
struct Edge
{
	int a, b, w;
}edge[M];
int cmp(Edge a, Edge b)
{
	return a.w < b.w;
}
void Kruskal()
{
	sort(edge, edge + m, cmp);
	for (int i = 1; i <= n; i++) {
		f[i] = i;
	}
	for (int i = 0; i < m; i++) {
		int a = edge[i].a;
		int b = edge[i].b;
		int w = edge[i].w;
		a = find(a);
		b = find(b);
		if (a != b) {
			f[a] = b;
			res += w;
			cnt++;
		}
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int a, b, w;
		cin >> a >> b >> w;
		edge[i] = { a,b,w };
	}
	Kruskal();
	if (cnt < n - 1) {
		cout << "impossible" << endl;
	}
	else {
		cout << res << endl;
	}
	return 0;
}